public class Solution112 {

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    boolean flag=false;

    public boolean hasPathSum(TreeNode root, int targetSum) {
        dfs(root,0,targetSum);
        return flag;
    }

    private void dfs(TreeNode root, int sum, int targetSum) {
        if (root==null){
            return;
        }
        if (root.left==null && root.right==null){
            flag|=(sum+root.val==targetSum);
            return;
        }
        dfs(root.left,sum+root.val,targetSum);
        dfs(root.right,sum+root.val,targetSum);
    }

    public static void main(String[] args) {
        System.out.println(new Solution112().hasPathSum(new TreeNode(5,new TreeNode(4,new TreeNode(11,new TreeNode(7),new TreeNode(2)),null),new TreeNode(8,new TreeNode(13),new TreeNode(4,null,new TreeNode(1)))),22));
    }


}
